using System;

namespace NMock2.Events
{
	/// <summary>
	/// This class is used for NMock2 style events.
	/// </summary>
	/// <remarks>
	/// This class is included in the NMock library to keep it together and complete.
	/// 
	/// It is, however, obsolete and a newer construct should be used.
	/// 
	/// Consider <see cref="MockEventInvoker"/>.
	/// 
	/// 
	/// 
	/// Code by Eric Willeke -- Keep my name here, please :) 
	/// http://manicprogrammer.com/cs/blogs/willeke/archive/2007/10/17/testing-delegates-with-nunit-and-nmock.aspx
	/// Adapted from original code by Neil Bourgeois - Edmonton, Alberta, Canada
	/// http://becomingagile.blogspot.com/2007/03/raising-events-in-nmock-20.html
	/// </remarks>
	[Obsolete("Consider using a MockEventInvoker to handle events.")]
	public class MockEvent<T> where T : class
	{
		private T handler;
		EventActivator<T> activatingDelegate;

		public MockEvent(EventActivator<T> activatingDelegate)
		{
			if (activatingDelegate == null)
				throw new ArgumentNullException();

			this.activatingDelegate = activatingDelegate;
		}

		public void Initialize(T handler)
		{
			if (handler == null)
			{
				throw new ArgumentNullException();
			}

			this.handler = handler;
		}

		public void Raise()
		{
			activatingDelegate(handler);
		}

		public static IAction Hookup(MockEvent<T> mockEvent)
		{
			return new MockEventHookup<T>(mockEvent);
		}
	}

	public delegate void EventActivator<T>(T handler);

}